package cn.sj.service.impl;

import cn.sj.mapper.SysMenuMapper;
import cn.sj.model.system.SysMenu;
import cn.sj.service.SysMenuService;
import cn.sj.util.MenuHelper;
import cn.sj.util.exception.OAException;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author asus
 * @version 1.0
 * @description: SysMenuServiceImpl
 * @date 2023/4/18 22:36
 */
@Service
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> implements SysMenuService {

    @Override
    public List<SysMenu> findNodes() {
        // 1、查询所有 的数据
        List<SysMenu> sysMenuList = baseMapper.selectList(null);

        // 2、构建树形结构
        List<SysMenu> list = MenuHelper.buildTree(sysMenuList);

        return list;
    }

    // 删除菜单
    @Override
    public void removeMenuById(Long id) {
        // 判断当前菜单是否有下一层菜单
        LambdaQueryWrapper<SysMenu> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysMenu::getParentId,id);

        Integer count = baseMapper.selectCount(lambdaQueryWrapper);

        if (count>0){
            throw new OAException(201,"菜单不能删除");
        }
        baseMapper.deleteById(id);
    }

}
